﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

using MatchDisp.Util;
using MatchDisp.MatchIterm;

namespace MatchDisp
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Image colorPre = null, colorPost = null;
        Image depthPre = null, depthPost = null;
        Rectangle colorPreBox = null, colorPostBox = null;
        Rectangle depthPreBox = null, depthPostBox = null;
        ItemContainer itemContainer = null;
        System.Drawing.Size imageSize;
        MatchItem preItem = null;

        SolidColorBrush StrokeBrush = new SolidColorBrush(Colors.Red);

        double scale = 1.0;

        public Window1()
        {
            InitializeComponent();
        }

        void moveBox(Rectangle box, System.Drawing.Rectangle rect)
        {
            box.Width = rect.Width;
            box.Height = rect.Height;
            Canvas.SetLeft(box, rect.X);
            Canvas.SetTop(box, rect.Y);
        }

        void repaint(MatchItem selectedItem)
        {
            // Change the position and sizes of the boxes
            if(selectedItem != null)
            {
                moveBox(colorPreBox, DrawingUtil.ScaleRect(selectedItem.GetPreMatchRect(), scale));
                moveBox(colorPostBox, DrawingUtil.ScaleRect(selectedItem.GetPostMatchRect(), scale));
                moveBox(depthPreBox, DrawingUtil.ScaleRect(selectedItem.GetPreMatchRect(), scale));
                moveBox(depthPostBox, DrawingUtil.ScaleRect(selectedItem.GetPostMatchRect(), scale));
                textBoxStatus.Text = string.Format("({0},{1},{2},{3}) - ({4},{5},{6},{7})", 
                    selectedItem.xPre, selectedItem.yPre, selectedItem.wPre, selectedItem.hPre,
                    selectedItem.xPost, selectedItem.yPost, selectedItem.wPost, selectedItem.hPost);
            }
            else
            {
//                 System.Drawing.Rectangle emptyRect = new System.Drawing.Rectangle(-3, -3, 1, 1);
//                 moveBox(colorPreBox, emptyRect);
//                 moveBox(colorPostBox, emptyRect);
//                 moveBox(depthPreBox, emptyRect);
//                 moveBox(depthPostBox, emptyRect);
//                textBoxStatus.Text = "score = ";
            }
        }

        void loadImages(String imagePath, String colorPathPre, String colorPathPost, String depthPathPre, String depthPathPost)
        {
            //             canvasColorPre.Background = new ImageBrush(new BitmapImage(new Uri(colorPathPre, UriKind.RelativeOrAbsolute))) 
            //                 { Stretch = System.Windows.Media.Stretch.Uniform };
            //             canvasColorPost.Background = new ImageBrush(new BitmapImage(new Uri(colorPathPost, UriKind.RelativeOrAbsolute))) 
            //                 { Stretch = System.Windows.Media.Stretch.Uniform};
            //             canvasDepthPre.Background = new ImageBrush(new BitmapImage(new Uri(depthPathPre, UriKind.RelativeOrAbsolute))) 
            //                 { Stretch = System.Windows.Media.Stretch.Uniform };
            //             if(File.Exists(depthPathPost))
            //                 canvasDepthPost.Background = new ImageBrush(new BitmapImage(new Uri(depthPathPost, UriKind.RelativeOrAbsolute)))
            //                 { Stretch = System.Windows.Media.Stretch.Uniform};

            // Load image
            double width = canvasColorPre.Width;
            colorPre = new System.Windows.Controls.Image() { Width = width, Source = new BitmapImage(new Uri(colorPathPre, UriKind.RelativeOrAbsolute)) };
            canvasColorPre.Children.Add(colorPre);
            colorPost = new System.Windows.Controls.Image() { Width = width, Source = new BitmapImage(new Uri(colorPathPost, UriKind.RelativeOrAbsolute)) };
            canvasColorPost.Children.Add(colorPost);
            depthPre = new System.Windows.Controls.Image() { Width = width, Source = new BitmapImage(new Uri(depthPathPre, UriKind.RelativeOrAbsolute)) };
            canvasDepthPre.Children.Add(depthPre);
            if (File.Exists(depthPathPost))
            {
                depthPost = new System.Windows.Controls.Image() { Width = width, Source = new BitmapImage(new Uri(depthPathPost, UriKind.RelativeOrAbsolute)) };
                canvasDepthPost.Children.Add(depthPost);
            }

            BitmapImage tmp = new BitmapImage(new Uri(colorPathPre, UriKind.RelativeOrAbsolute));
            imageSize = new System.Drawing.Size((int)(tmp.Width), (int)(tmp.Height));
            scale = width / tmp.Width;

            // Create box
            colorPreBox = new Rectangle() { Height = 1, Width = 1, Stroke = StrokeBrush};
            Canvas.SetLeft(colorPreBox, -3);
            colorPostBox = new Rectangle() { Height = 1, Width = 1, Stroke = StrokeBrush};
            Canvas.SetLeft(colorPostBox, -3);
            depthPreBox = new Rectangle() { Height = 1, Width = 1, Stroke = StrokeBrush};
            Canvas.SetLeft(depthPreBox, -3);
            depthPostBox = new Rectangle() { Height = 1, Width = 1, Stroke = StrokeBrush};
            Canvas.SetLeft(depthPostBox, -3);
            canvasColorPre.Children.Add(colorPreBox);
            canvasColorPost.Children.Add(colorPostBox);
            canvasDepthPre.Children.Add(depthPreBox);
            canvasDepthPost.Children.Add(depthPostBox);
        }

        void loadData(String dataPath)
        {
            itemContainer = new ItemContainer(dataPath, imageSize, scale);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            string dataPath, colorPathPre, colorPathPost, depthPathPre, depthPathPost;
            if ((dataPath = FormsUtil.GetFilePathFromOpenFileDialog(StringUtil.TXT_FILTER,
                FormsUtil.DialogType.OpenDialog)) == null)
                return;
            if ((colorPathPre = FormsUtil.GetFilePathFromOpenFileDialog(StringUtil.IMAGE_FILTER,
                FormsUtil.DialogType.OpenDialog)) == null)
                return;
            if ((colorPathPost = FormsUtil.GetFilePathFromOpenFileDialog(StringUtil.IMAGE_FILTER,
                            FormsUtil.DialogType.OpenDialog)) == null)
                return;

            string tmpSurfix = StringUtil.GetSurfix(colorPathPre, "\\");
            string tmpPrefix = StringUtil.GetPrefix(colorPathPre, "\\");
            if (tmpPrefix.Length != 0)
            {
                depthPathPre = tmpPrefix + "\\depth" + tmpSurfix.Substring(5);
                depthPathPost = tmpPrefix + "\\depth" + colorPathPost.Substring(5);
            }
            else
            {
                depthPathPre = "depth" + tmpSurfix.Substring(5);
                depthPathPost = "depth" + colorPathPost.Substring(5);
            }

            loadImages(dataPath, colorPathPre, colorPathPost, depthPathPre, depthPathPost);
            loadData(dataPath);

            repaint(null);
        }

        private void canvasColorPre_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Point pos = e.GetPosition(colorPre);
            MatchItem item = itemContainer.SelectItem((int)(pos.X), (int)(pos.Y));
            repaint(item);
        }

        private void canvasColorPre_MouseMove(object sender, MouseEventArgs e)
        {
            Point pos = e.GetPosition(colorPre);
            MatchItem item = itemContainer.SelectItem((int)(pos.X), (int)(pos.Y));
            if (item == preItem)
                return;
            preItem = item;
            repaint(item);
        }
    }
}

